$NG.AllReady(function (page, { useValuesChange, useUpdateRow, useUpdateRows }) {
    //获取控件
    var form = $NG.getCmpApi("p_form_equipment_list_split");
    var grid = $NG.getCmpApi("p_form_equipment_split_d");
    var grid2 = $NG.getCmpApi("p_form_equipment_split_dd");
    const dataMaps_key = {};
    //表体增行时设置默认值，当表头u_split_rate变化时，更新所有明细数据
    useValuesChange(({ args }) => {
      console.log("useValuesChange is reayd");
      console.log(args[0]);
      var u_split_rate = $NG.getCmpApi("p_form_equipment_list_split").getItem("u_split_rate").getValue();
      var grid = $NG.getCmpApi("p_form_equipment_split_d");
      // 获取表体数据
      var data = grid.getData(); // 假设这个方法能获取到表体数据
      console.log("切分获取表格数据", data);
      // 遍历数据并设置 u_split_rate 为 1
      // data.forEach(function (item) {
      //     item.u_split_rate = u_split_rate; // 设置 u_split_rate 为 1
      //     sum(item)
      // });
      data.forEach((item) => {
        item.u_split_rate = form.getItem("u_split_rate").getValue();
        sum(item);
      });
      const _data = [...data];
      grid.clearRows().then(() => {
        grid.addRows(_data);
      });
      caculatedgrid();
    }, "p_form_equipment_list_split.u_split_rate");
  
    useUpdateRow(({ args }) => {

      caculatedgrid();
    }, "p_form_equipment_split_d");
    useUpdateRows(({ args }) => {
      console.log(`表格新增监听`, args);
      setSpiltAmt()
      caculatedgrid();
      reduceSum()
      grid.refreshView();
    },"p_form_equipment_split_d");

    //设置切分比例
    function setSpiltAmt(){
      const arr = grid.getRows()
      arr.forEach(function (item) {
        selectClass(item.u_revenue_type,(data)=>{
          item.u_split_rate = data.u_split_rate
          sum(item)
          grid.refreshView()
        })
      });
    }

    useUpdateRows(({ args }) => {
      setSpiltAmt()
      reduceSum()
    },"p_form_equipment_split_dd");
    setInterval(() => {
      try {
        if (grid.getData().length !== dataMaps_key["danjv"]) {
          dataMaps_key["danjv"] = grid.getData().length;
          const data = grid.getData();
          data.forEach((item) => {
            sum(item);
          });
          caculatedgrid();
          console.log("可能发生了单据引用");
        }
      } catch (err) {}
    }, 500);
    function caculatedgrid() {
      let sum_amt_vat = 0;
      let sum_amt = 0;
      const DRows = $NG.getCmpApi("p_form_equipment_split_d").getRows();
      DRows.forEach((items) => {
        sum_amt_vat += parseFloat(items.u_split_amt_vat) || 0;
        sum_amt += parseFloat(items.u_split_amt) || 0;
      });
      $NG.updateState((updater) => {
        updater.data.p_form_equipment_list_split.setProps({
          u_split_amt_vat: sum_amt_vat,
          u_split_amt: sum_amt,
        });
      });
    }
    function sum(item) {
      // 原未税单价 = 原含税单价 / （1+税率）
      item.u_ori_prc = Number(item.u_ori_prc_vat) / (1 + Number(item.u_tax_rate));
      // 切分含税单价 = 切分比例 * 原含税单价
      item.u_split_prc_vat = Number(item.u_ori_prc_vat) * Number(item.u_split_rate);
      // 切分未税单价 = 切分含税单价 / （1+税率）
      item.u_split_prc = (Number(item.u_ori_prc_vat) * Number(item.u_split_rate)) / (1 + Number(item.u_tax_rate));
      //切分含税金额
      item.u_split_amt_vat = (Number(item.u_qty) || 0) * (Number(item.u_split_prc_vat) || 0);
      // 切分未税金额 = 数量 * （切分含税单价 / （1 + 税率））
      item.u_split_amt = Number(item.u_qty) * ((Number(item.u_ori_prc_vat) * Number(item.u_split_rate)) / (1 + Number(item.u_tax_rate)));
      // 切分税额 = 切分未税金额 * 税率， 将切分未税金额的计算公式带入可得：切分税额 = 数量 * （切分含税单价 /（1 + 税率）） * 税率
      item.u_tax_amt = Number(item.u_qty) * (Number(item.u_split_prc_vat) / (1 + Number(item.u_tax_rate))) * Number(item.u_tax_rate);
      grid.refreshView();
    }

    //查切分划分
    function selectClass(phid,fun){
        const _data = grid2.getRows()
        console.log(_data,'dg2',phid);
        let _temp = null
        try{
            _data.forEach(item=>{
                if(item.u_revenue_type == phid){
                    _temp = item
                    throw Exception("跳出forEach")
                }
            })
            fun({})
        }catch(err){
            console.log(`查询切分成功`);
            fun(_temp)
        }
    }

    //汇总含税金额
    function reduceSum(){
      const map = {}
      const dg1row = grid.getRows()
      dg1row.forEach(item=>{
        if(map[item.u_revenue_type]){
          map[item.u_revenue_type]+=Number(item.u_split_amt_vat)
        }else{
          map[item.u_revenue_type]=Number(item.u_split_amt_vat)
        }
      })
      const dg2row = grid2.getRows()
        dg2row.forEach(item=>{
          if(map[item.u_revenue_type]){
            item.u_split_amt_vat = map[item.u_revenue_type]

          } else {
            item.u_split_amt_vat = 0
          }
        })
      console.log(`金额`, map);
      grid2.refreshView()
    }
  });
  